package com.glory.dgcpcpda.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;

import java.util.List;
/**
 * @author hx
 * @date 2019/11/15
 * @desc
 */
public class MyUtils {

    public interface  EditTextActionListener{
        void onKeyDown(String s);
    }

    public static List<FreeUI_TabIndexGridEntity> setPicToDrawable(Context context, List<FreeUI_TabIndexGridEntity> entity) {
        for (FreeUI_TabIndexGridEntity og : entity) {
            if (og.getPicName().length() != 0) {
                Drawable drawable = context.getResources()
                        .getDrawable(context.getResources()
                                .getIdentifier(og.getPicName(), "drawable", context.getPackageName()));
                og.setDrawable(drawable);
            }
        }
        return entity;
    }

    public static void editActionListener(final EditText editText, final EditTextActionListener listener){
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                //当actionId == XX_SEND 或者 XX_DONE时都触发
                //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
                if (actionId == EditorInfo.IME_ACTION_SEND
                        || actionId == EditorInfo.IME_ACTION_NEXT
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || (keyEvent != null && KeyEvent.KEYCODE_ENTER == keyEvent.getKeyCode() && KeyEvent.ACTION_DOWN == keyEvent.getAction())) {
                    //处理事件
                    listener.onKeyDown(editText.getText().toString().trim());
                }
                return false;
            }
        });
    }

}
